package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbContentCategoryMapper;
import com.dongdongshop.model.TbContentCategory;
import com.dongdongshop.service.ContentCategoryService;
import com.dongdongshop.vo.ContentCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

    @Autowired
    private TbContentCategoryMapper tcm;

    @Override
    public List<ContentCategoryVO> selectAll() {
        List<TbContentCategory> categoryList = tcm.selectByExample(null);
        return categoryList.stream().map(tbContentCategory -> {
            ContentCategoryVO vo = new ContentCategoryVO();
            BeanUtils.copyProperties(tbContentCategory,vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public void addCategory(ContentCategoryVO vo) {
        TbContentCategory category = new TbContentCategory();
        BeanUtils.copyProperties(vo,category);
        tcm.insertSelective(category);
    }

    @Override
    public ContentCategoryVO toUpdate(Long id) {
        TbContentCategory category = tcm.selectByPrimaryKey(id);
        ContentCategoryVO vo = new ContentCategoryVO();
        BeanUtils.copyProperties(category,vo);
        return vo;
    }

    @Override
    public void update(ContentCategoryVO vo) {
        TbContentCategory category = new TbContentCategory();
        BeanUtils.copyProperties(vo,category);
        tcm.updateByPrimaryKeySelective(category);
    }

    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            tcm.deleteByPrimaryKey(id);
        }
    }
}
